#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :抖音基于搜索词获取up主.py
# @Time      :2024/6/5 
# @Author    :CL
# @email     :1037654919@qq.com
import requests

def get_ups(keyword ="科学"):
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://www.douyin.com/search/191.%E8%BF%99%E4%B8%8D%E7%A7%91%E5%AD%A6%E5%95%8A?type=user",
        "sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
    }
    cookies = {
        "FORCE_LOGIN": "%7B%22videoConsumedRemainSeconds%22%3A180%7D",
        "passport_csrf_token": "f25491b101c7f4d197ff9e3dd4e773ff",
        "passport_csrf_token_default": "f25491b101c7f4d197ff9e3dd4e773ff",
        "s_v_web_id": "verify_lwzsnoqj_la0iG5qb_oCxU_4pBr_A7l9_dRvonHR4hm8E",
        "ttwid": "1%7C3oUPgcKtas8DrPpD8EBuN7a_ghLj3ZWMeF22tjSGkq8%7C1717468947%7Ca1f00155b84e9393a7d9c2c8f395a11542ba7d074f2590e65d80a52a6b145eb0",
        "dy_swidth": "1920",
        "dy_sheight": "1080",
        "bd_ticket_guard_client_web_domain": "2",
        "d_ticket": "fffd8ce1ffb2bdeb7fe424eaec92c8bf3d866",
        "n_mh": "bhAniSCSHde_ORn1DHS1y6_YqpZdSmu2xUpT3aD7_bY",
        "publish_badge_show_info": "%220%2C0%2C0%2C1717469143467%22",
        "_bd_ticket_crypt_doamin": "2",
        "__security_server_data_status": "1",
        "download_guide": "%220%2F%2F1%22",
        "volume_info": "%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.5%7D",
        "pwa2": "%220%7C0%7C3%7C0%22",
        "SEARCH_RESULT_LIST_TYPE": "%22multi%22",
        "SearchMultiColumnsVisitedTags": "%5B%22for_discover_search-0%22%2C%22for_discover_search-1%22%5D",
        "douyin.com": "",
        "device_web_cpu_core": "8",
        "device_web_memory_size": "8",
        "architecture": "amd64",
        "csrf_session_id": "867881e6aab7d3374b2c508067949392",
        "strategyABtestKey": "%221717550733.113%22",
        "passport_assist_user": "Cj1AjSx0sjlmZ6p78P-vqMkA6Mmf6m9Cv817Dxa9qENw8m5tT8sUUY4Vf-eWQPdRsEXGaUFmuKVAyWHawca5GkoKPGdSIYPFwE6ga7DDPxa6VKCaDKHOeRAi5vTyPfe3aU7T545s8g_wYt3HDA0R-tiOdAB1ukkA63nFaTR3exCbldMNGImv1lQgASIBA4L_Ls4%3D",
        "sso_uid_tt": "07c9ef1e39e9d6a3c9cbfe13ad5344b3",
        "sso_uid_tt_ss": "07c9ef1e39e9d6a3c9cbfe13ad5344b3",
        "toutiao_sso_user": "f9afeb32c2281d1712a2bacf3db26325",
        "toutiao_sso_user_ss": "f9afeb32c2281d1712a2bacf3db26325",
        "sid_ucp_sso_v1": "1.0.0-KDFjZDFmYTAwODgxY2M3Yzk1Y2U4YWFmNWQwZjlkY2E4MGRlMzQwNjkKHwjJ4q7LiAMQ6f7-sgYY7zEgDDDp4aXeBTgGQPQHSAYaAmxmIiBmOWFmZWIzMmMyMjgxZDE3MTJhMmJhY2YzZGIyNjMyNQ",
        "ssid_ucp_sso_v1": "1.0.0-KDFjZDFmYTAwODgxY2M3Yzk1Y2U4YWFmNWQwZjlkY2E4MGRlMzQwNjkKHwjJ4q7LiAMQ6f7-sgYY7zEgDDDp4aXeBTgGQPQHSAYaAmxmIiBmOWFmZWIzMmMyMjgxZDE3MTJhMmJhY2YzZGIyNjMyNQ",
        "passport_auth_status": "32ba0591a24f82658ed317304b309088%2C2362f547bfa52b74723bbe8a3ad8a64f",
        "passport_auth_status_ss": "32ba0591a24f82658ed317304b309088%2C2362f547bfa52b74723bbe8a3ad8a64f",
        "uid_tt": "af3179809332176bbc032c9503a733db",
        "uid_tt_ss": "af3179809332176bbc032c9503a733db",
        "sid_tt": "80c7d279e7e08130696a182725e6442c",
        "sessionid": "80c7d279e7e08130696a182725e6442c",
        "sessionid_ss": "80c7d279e7e08130696a182725e6442c",
        "_bd_ticket_crypt_cookie": "72d4d999652f2f3f1b9fabc3a2f5aa77",
        "sid_guard": "80c7d279e7e08130696a182725e6442c%7C1717550958%7C5183998%7CSun%2C+04-Aug-2024+01%3A29%3A16+GMT",
        "sid_ucp_v1": "1.0.0-KDVmMWEwNTZhNmMyNWZhMmQzNzc4ZGUwOTEzMmMwNTQyODJjZWU3NTIKGQjJ4q7LiAMQ7v7-sgYY7zEgDDgGQPQHSAQaAmhsIiA4MGM3ZDI3OWU3ZTA4MTMwNjk2YTE4MjcyNWU2NDQyYw",
        "ssid_ucp_v1": "1.0.0-KDVmMWEwNTZhNmMyNWZhMmQzNzc4ZGUwOTEzMmMwNTQyODJjZWU3NTIKGQjJ4q7LiAMQ7v7-sgYY7zEgDDgGQPQHSAQaAmhsIiA4MGM3ZDI3OWU3ZTA4MTMwNjk2YTE4MjcyNWU2NDQyYw",
        "__ac_nonce": "0665ffe5d00000edd2db2",
        "__ac_signature": "_02B4Z6wo00f010w2h0wAAIDBXphxm2wE9BtMFoPAALVd53",
        "FOLLOW_LIVE_POINT_INFO": "%22MS4wLjABAAAAjnHMUHpTcYt0jvUiDdiDDBxKyuh9zFFfBBJckLKzCcI%2F1717603200000%2F0%2F0%2F1717568378111%22",
        "FOLLOW_NUMBER_YELLOW_POINT_INFO": "%22MS4wLjABAAAAjnHMUHpTcYt0jvUiDdiDDBxKyuh9zFFfBBJckLKzCcI%2F1717603200000%2F0%2F0%2F1717568978112%22",
        "stream_recommend_feed_params": "%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1920%2C%5C%22screen_height%5C%22%3A1080%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A100%7D%22",
        "xg_device_score": "7.34126709790992",
        "stream_player_status_params": "%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A0%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A0%7D%22",
        "bd_ticket_guard_client_data": "eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCR1BQUXBtcG5YcW00UkRHS0JwYjRuWlR4Z2drMENJOUw0Y0Y1OFNBTnlWTEN5TWVmNUw3cXZaMmFybFpNL0xxYWhuME00ZGFnTldRL1NWQlNMdkY3UXM9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D",
        "odin_tt": "5d48e7ca688aa9468f6ec955916f758c6cf7f8904c014506cee1e4eeef6c9265eefc08aa957b6e973b41529f0bdf2242",
        "msToken": "nBRRWRinRanP6qOzXl0CGFBtt9T8yYrqsOmAI3J6t1TgPNR3wQlW9Cas2x74gtnxMZ2hsaRTHP-1aTu5sGa604BUZwO5tQLK5bsm6NRhDGrn_KABRLvCOSyFp4NmJhXf",
        "passport_fe_beating_status": "false",
        "home_can_add_dy_2_desktop": "%220%22",
        "IsDouyinActive": "true"
    }
    url = "https://www.douyin.com/aweme/v1/web/discover/search/"
    params = {
        "device_platform": "webapp",
        "aid": "6383",
        "channel": "channel_pc_web",
        "search_channel": "aweme_user_web",
        "keyword":keyword ,
        "search_source": "normal_search",
        "query_correct_type": "1",
        "is_filter_search": "0",
        "from_group_id": "",
        "offset": "0",
        "count": "12",
        "need_filter_settings": "1",
        "list_type": "multi",
        "update_version_code": "170400",
        "pc_client_type": "1",
        "version_code": "170400",
        "version_name": "17.4.0",
        "cookie_enabled": "true",
        "screen_width": "1920",
        "screen_height": "1080",
        "browser_language": "zh-CN",
        "browser_platform": "Linux x86_64",
        "browser_name": "Chrome",
        "browser_version": "124.0.0.0",
        "browser_online": "true",
        "engine_name": "Blink",
        "engine_version": "124.0.0.0",
        "os_name": "Linux",
        "os_version": "x86_64",
        "cpu_core_num": "8",
        "device_memory": "8",
        "platform": "PC",
        "downlink": "10",
        "effective_type": "4g",
        "round_trip_time": "150",
        "webid": "7376472877694109247",
        "msToken": "scmYUhv7lqbz9k6keSZa88EJKkmLWJKdOuAHF0PCFokac-nwk3mW0vx_w5PxaczXeuXtqCc4syrAiTx4Yl5PI9Hcaa_yB4MjylvCQwU3VIdwNCicfV-wVCAYafSiTuW0",
        "a_bogus": "xJmqBDu6dDgphDSv5I/LfY3q6la3YkGC0CPYMD2fld3GJy39HMTw9exoWh4vpCDjLG/lIeRjy4hbO3xprQC7MZwf7Wsx/2CZQg00tMeg5xSSs1feejusJUsNmktlCec25kHUEKi8q7CaSYumAoFe4Xn6bfebYrtswrDYtn1ndVwd9ULQktD="
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.url,response)
    if response.status_code == 200:
        return response.json()


if __name__ == '__main__':
    keyword = input("请输入搜索关键词：")
    get_ups(keyword)